package com.example.hp.screenrose;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends Activity {
    private static MediaPlayer alarmMusic = null;
    private RelativeLayout rose_layout;
    private int mWidth, mHeight;
    int X_index = 0;
    int Y_index = 0;
    Random rand = new Random();
    private int[] picArrary = {R.drawable.rose0, R.drawable.rose1, R.drawable.rose2, R.drawable.rose3,
            R.drawable.rose4, R.drawable.rose5,};
    private int withSpace, heighSpace; //高宽中每个图片的间隔
    private int RoseCount, wCount, hCount; //画图片计数
    private final static int wRoseCount = 5; //横向画图片个数
    private final static int hRoseCount = 8; //纵向画图片个数
    private int[] indexList = new int[wRoseCount * hRoseCount]; //存放产生的随机坐标
    private int textsize;
    private final static int times = 500; //每张图出现的间隔时间，单位：毫秒
    private final static String[] mText = {"鲜", "花", "", "在", "送", "你", "之", "前", "只", "是", "鲜", "花", ""
            , "在", "你", "接", "手", "笑", "过", "之", "后", "", "它", "生", "根", "了", "", "在", "我", "心", "里",
            "。", "。", "。"}; //要显示的文字
    private final static int maxText = 9; //每行显示字数最大值
    private final static int maxLine = 5; //显示行数
    private String TextColer = "#13ed37"; //文字颜色
    private android.os.Handler mHandler = new android.os.Handler();
    int X_text = 0;
    int Y_text = 0;
    TextView tv;
    String str = "";
    int j = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.rose_layout);
        dataInit();
        viewInit();
        playMusic();
    }

    private void viewInit() {
        rose_layout = (RelativeLayout) findViewById(R.id.rose_layout);
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        mWidth = wm.getDefaultDisplay().getWidth();
        mHeight = wm.getDefaultDisplay().getHeight();

        withSpace = mWidth / wRoseCount - 2;
        heighSpace = mHeight / hRoseCount - 2;
        RoseCount = 0;
        mHandler.postDelayed(mRunnable, 0);
    }

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            X_index = (indexList[RoseCount] % wRoseCount) * withSpace - withSpace / 6;
            Y_index = (indexList[RoseCount] / wRoseCount) * heighSpace - heighSpace / 6;

            int x = rand.nextInt(5) * (rand.nextInt(2) == 1 ? -1 : 1) * 15;
            int y = rand.nextInt(5) * (rand.nextInt(2) == 1 ? -1 : 1) * 15;

            ImageView view = new ImageView(MainActivity.this);
            view.setBackground(getResources().getDrawable(picArrary[rand.nextInt(picArrary.length)]));
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) (withSpace * 1.5), (int) (withSpace * 1.5));
            lp.setMargins(X_index - x, Y_index - y, -withSpace / 6 - Math.abs(x), -withSpace / 6 - Math.abs(y));
            rose_layout.addView(view, lp);

            RoseCount++;
            if (RoseCount < wRoseCount * hRoseCount) {
                mHandler.postDelayed(mRunnable, 800);
            } else {
                textsize = mWidth / (maxText * 3);
                X_text = textsize / 2;
//                Y_text = (mHeight / textsize - maxLine) / 2 * textsize;
                Y_text = mHeight / 4;
                tv = new TextView(MainActivity.this);
                tv.setTextSize(textsize);
                tv.setTextColor(Color.parseColor(TextColer));
                tv.setLineSpacing(textsize / 2, 1);
                RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp1.setMargins(X_text, Y_text, 0, 0);
                rose_layout.addView(tv, lp1);
                str = "";
                j = 0;
                mHandler.postDelayed(textRunnable, 500);
            }
        }
    };

    Runnable textRunnable = new Runnable() {
        @Override
        public void run() {
            str += mText[j];
            tv.setText(str);
            if (mText[j].equals("")) {
                str += '\n';
            }
            j++;
            if (j < mText.length) {
                mHandler.postDelayed(textRunnable, 500);
            } else {
                //显示贺卡
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                ImageView imge = new ImageView(MainActivity.this);
//                imge.setBackground(getResources().getDrawable(R.drawable.card));
//                RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mWidth / 3 * 2);
//                lp1.setMargins(0, mHeight / 4, 0, 0);
//                rose_layout.addView(imge, lp1);
            }
        }
    };


    private void dataInit() {
        for (int i = 0; i < indexList.length; i++) {
            indexList[i] = i;
        }
        for (int i = 0; i < indexList.length; i++) {
            int t = rand.nextInt(indexList.length - 1) % (indexList.length - i) + i;
            int a = indexList[i];
            indexList[i] = indexList[t];
            indexList[t] = a;
        }
    }

    private void playMusic() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (alarmMusic == null) {
                    alarmMusic = MediaPlayer.create(MainActivity.this, R.raw.music);
                    alarmMusic.setLooping(false);
                    alarmMusic.start();
                }
            }
        }).start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        finish();
        onBackPressed();
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        if (null != alarmMusic) {
            alarmMusic.stop();
            alarmMusic.release();
            alarmMusic = null;
        }
        super.onDestroy();
    }
}
